package Symulator2009;
import java.awt.Color;
import java.awt.Graphics;

class Light{
	private	int x;
	private int y;
	private boolean a; //green shining
	private boolean b; //red shining
	private boolean isnormal;
	private int start;
	private int green;
	private int red;
	private int orange;
	private long time;
	private boolean visible;
	//
	private int stopXa;
	private int stopYa;
	private int stopXb;
	private int stopYb;
	//
	private int countA;//podaje ilosc samochodow na skrzyzowaniu jesli jest > 0 a zmienia sie swiatla ruch zostanie zatrzymany
	private int countB;
	public int getCountA() {
		return countA;
	}
	public void setCountA(int counta) {
		countA=counta;
	}
	public int getCountB() {
		return countB;
	}
	public void setCountB(int countb) {
		countB=countb;
	}
	Light(int x, int y, int green, int red, int start, boolean isnormal,long time,int stopxa,int stopxb,int stopya,int stopyb,boolean vis)
	{
		this.x=x;
		this.y=y;
		this.green=green;
		this.orange=Data.getOrangetime();
		this.red=red;
		this.start=start;
		this.isnormal=isnormal;
		this.time=time;
		this.a=true;
		this.b=false;
		this.stopXa=stopxa;
		this.stopXb=stopxb;
		this.stopYa=stopya;
		this.stopYb=stopyb;
		this.visible=vis;
		this.countA=0;
		this.countB=0;
	}
	public boolean getA(){return a;}
	public boolean getB(){return b;}
	//metoda do ustawienia pozniejszego czasu startowego aby jeszcze mniej zminimalizowac bledy powstale w skutek czasu ktory
	//uplywa podczas tworzenia obiektow
	public void setTime(long t){ this.time=t; }
	//ustawia a i b sa zle swiatla swieca dobrze 
	void update2(Graphics g){
		//bledy w rozumowaniu
//	if(((System.currentTimeMillis()-time)/1000+start)%(green+red+orange)<=green)
//		if(isnormal)
//		{
//			a=true;
//			greenLight(g);
//		}
//		else
//		{
//			a=false;
//			redLight(g);
//		}
//	else if(((System.currentTimeMillis()-time)/1000+start)%(green+red+orange)>green+orange)
//		if(isnormal)
//		{
//			a=false;
//			redLight(g);
//		}
//		else
//		{
//			a=true;
//			greenLight(g);
//		}
//	else
//		{
//			orangeLight(g);
//		}
//	//if//na nowo napisac z nowymi poprawkami
		if(visible){
		if(isnormal)
			if(((System.currentTimeMillis()-time)/1000+start)%(green+red+orange)<=green)
			{
				a=true;
				//b=false;
				greenLight(g);
			}
			else if (((System.currentTimeMillis()-time)/1000+start)%(green+red+orange)>green+orange)
			{
				a=false;
				//if(((System.currentTimeMillis()-time)/1000+start)%(green+red+orange)<=green+red)
				//	b=true;
				//else
				//	b=false;
				redLight(g);
			}
			else
			{
				a=false;
				//b=false;
				orangeLight(g);
			}
		else
			if(((System.currentTimeMillis()-time)/1000+start)%(green+red+orange)<=green+orange)
			{
				a=false;
				//if(((System.currentTimeMillis()-time)/1000+start)%(green+red+orange)<=green)
				//	b=true;
				//else
				//	b=false;
				redLight(g);
			}
			else if (((System.currentTimeMillis()-time)/1000+start)%(green+red+orange)>green+red)
			{
				a=false;
				orangeLight(g);
				//b=false;
			}
			else
			{
				a=true;
				greenLight(g);
				//b=false;
			}
		}
		else
		{
			a=true;
			b=true;
		}
	}
	void update(Graphics g){
		
		if(visible){
				if(((Data.getIterations()*Data.getReloadSpeed())/1000+start)%(green+red+2*orange)<=green)
				{
					a=true;
					b=false;
					greenLight(g);
				}
				else if (((Data.getIterations()*Data.getReloadSpeed())/1000+start)%(green+red+2*orange)>green+orange && ((Data.getIterations()*Data.getReloadSpeed())/1000+start)%(green+red+2*orange)<=green+orange+red)
				{
					a=false;
					b=true;
					//if(((System.currentTimeMillis()-time)/1000+start)%(green+red+orange)<=green+red)
					//	b=true;
					//else
					//	b=false;
					redLight(g);
				}
				else
				{
					a=false;
					b=false;
					//b=false;
					orangeLight(g);
				}
		}
		
	}
	
	void setLight(Graphics g){
		 	if(visible){
		    Color c=g.getColor();
		    g.setColor(Color.black);
		    g.drawRect(x-1, y-1, 18, 50);
		    g.drawOval(x, y, 16, 16);
		    g.drawOval(x, y+16, 16, 16);
		    g.drawOval(x, y+32, 16, 16);
		    g.setColor(c);
		 	}
		    }
		    
	private void greenLight(Graphics g){
		 
		    Color c=g.getColor();
		    g.setColor(Color.green);
		    g.fillOval(x+1, y+1, 15, 15);
		    g.setColor(Color.white);
		    g.fillOval(x+1, y+17, 15, 15);
		    g.fillOval(x+1, y+33, 15, 15);
		    g.setColor(c);
		    }
		    
		    
	private void orangeLight(Graphics g){ 
		
		    Color c=g.getColor();
		    g.setColor(Color.orange);
		    g.fillOval(x+1, y+17, 15, 15);
		    g.setColor(Color.white);
		    g.fillOval(x+1, y+1, 15, 15);
		    g.fillOval(x+1, y+33, 15, 15);
		    g.setColor(c);
		       
		    }
		    
	private void redLight(Graphics g){
		    	
		    Color c=g.getColor();
		    g.setColor(Color.red);
		    g.fillOval(x+1, y+33, 15, 15);
		    g.setColor(Color.white);
		    g.fillOval(x+1, y+1, 15, 15);
		    g.fillOval(x+1, y+17, 15, 15);
		    g.setColor(c);
		       
		    }
	public int getstopXb() {
		return stopXb;
	}
	public int getstopYb() {
		return stopYb;
	}
	public int getstopXa() {
		return stopXa;
	}
	public int getstopYa() {
		return stopYa;
	}
}